#include <iostream>
#include <string>
#include <memory>
#include "Socket.hpp"
#include "protocol.hpp"
using namespace std;
using namespace Net_Work;


int main(int argc, char* args[]) {
    if (argc != 3) {
        cout << "Usage:\n\r" << args[0] << " server_ip server_port\n" << endl;
        return 0;
    }
    string serverip = args[1];
    uint16_t serverport = stoi(args[2]);
    unique_ptr<Socket> socket(new TcpSocket());
    socket->BuildConnectSocketMethod(serverip, serverport);
    string send_message;
    unique_ptr<Factory> fact(new Factory());
    auto resp = fact->BuildResponse();
    while (true) {
        cout << "Please Enter# ";
        getline(cin, send_message);
        if (send_message == "quit") {
            break;
        }
        if (send_message.empty()) {
            continue;
        }
        // 封装成完成报文
        send_message = Encode(send_message);
        socket->Send(send_message);

        // 接收服务器发回
        string respStr;
        socket->Recv(&respStr, 1024);
        // 解包
        string resp_message;
        while (Decode(respStr, &resp_message)) {
            // 反序列化
            if (!resp->DeSerialize(resp_message) ) {
                continue;
            }
            cout << "result--> " << resp->ToString() << endl;
        }
    }

    return 0;
}